package com.example.phone.base;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

/**
 * Created by Administrator on 2018\12\24 0024.
 */

/**
 * fragment的基类，封装常用方法
 */
public abstract class BaseFragment extends Fragment {
    private View mContentView;
    private Toast toast;
    protected final String TAG = this.getClass().getSimpleName();

    /**
     * 调试开关，与activity中保持一致
     */
    protected final boolean DEBUG = BaseActivity.DEBUG;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mContentView = inflater.inflate(initLayout(), container, false);
        init();
        initView();
        initData();
        log("*********Fragment*******" + TAG);
        return mContentView;
    }

    /**
     * 打印日志
     * @param msg 要打印的消息
     */
    public void log(String msg) {
        if(DEBUG)
            Log.d(TAG, msg);
    }

    /**
     * toast弹窗
     * @param msg 窗体消息
     */
    public void toastShort(String msg) {
        if (null == toast) {
            toast = Toast.makeText(getContext(), msg, Toast.LENGTH_SHORT);
            toast.show();
        } else {
            toast.setText(msg);
            toast.show();
        }
    }

    /**
     * 初始化视图
     */
    protected abstract void initView();

    /**
     * 初始化数据
     */
    protected abstract void initData();

    /**
     * 初始化操作
     */
    protected abstract void init();

    /**
     * 设置Fragment布局
     * @return 布局id
     */
    protected abstract int initLayout();

    /**
     * activity跳转
     * @param cls 要跳转的活动
     */
    protected void goNextActivity(Class<?> cls) {
        Intent intent = new Intent(getContext(),cls);
        startActivity(intent);
    }

    /**
     * activity跳转
     * @param cls 要跳转的活动
     * @param bundle 传递到跳转活动的数据
     */
    protected void getNextActivity(Class<?> cls, Bundle bundle) {
        Intent intent = new Intent(getContext(), cls);
        intent.putExtras(bundle);
        startActivity(intent);
    }

    protected View getmContentView() {
        return mContentView;
    }
}
